package com.bank.bank.account.controller;


import com.bank.bank.account.service.AccountService;
import com.bank.bank.common.base.pojo.dto.AccountAddParam;
import com.bank.bank.common.base.pojo.dto.AccountDTO;
import com.bank.bank.common.base.pojo.entity.Account;
import com.bank.bank.common.base.pojo.vo.AccountTransactionVO;
import com.bank.bank.common.base.pojo.vo.AccountUpdateVO;
import com.bank.bank.common.base.response.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/accounts")
public class AccountController {
    //自动装配
    @Autowired
    private AccountService accountService;

    /**
     * 1.开户功能
     * @param accountAddParam
     * @return
     */
    @PostMapping("/add")
    public JsonResult addAccount(@RequestBody AccountAddParam accountAddParam){
        log.debug("accountAddParam:{}", accountAddParam);
        Account account = accountService.addAccount(accountAddParam);
        return JsonResult.ok(account);
    }

    /**
     * 2.查询账户列表
     */
    @GetMapping("/list")
    public JsonResult listAccounts(Integer customerId){
        List<Account> accountList = accountService.listAccounts(customerId);
        return JsonResult.ok(accountList);
    }

    /**
     * 3.注销账户功能
     */
    @PostMapping("/{accountId}/del")
    public JsonResult delAccount(@PathVariable("accountId") Long accountId){
        log.debug("accountId:{}", accountId);
        accountService.delAccount(accountId);
        return JsonResult.ok();
    }

    /**
     * 4.查询账户交易详情功能
     */
    @GetMapping("/{accountId}/transactions")
    public JsonResult getAccountTransactions(@PathVariable("accountId") Integer accountId){
        log.debug("accountId:{}", accountId);
        AccountTransactionVO accountTransactionVO = accountService.getAccountTransactions(accountId);
        return JsonResult.ok(accountTransactionVO);
    }

    /**
     * trans调用 更新且查询最新的账户余额状态接口
     */
    @GetMapping("/update")
    public JsonResult updateAndGetAccounts(Integer accountId,Double balance){
        //控制层 调用业务层的时候 返回什么 就让业务层返回什么
        AccountUpdateVO vo=accountService.updateAndGetAccount(accountId,balance);
        return JsonResult.ok(vo);
    }
    /*
     服务内部调用接口 返回不在使用JsonResult
     */
    @GetMapping("/{id}")
    public AccountDTO getAccountById(@PathVariable Integer id){
        return accountService.getAccountById(id);
    }
}












